package com.yunyi.integration.action;


import cn.hutool.core.date.DateUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.*;
import com.yunyi.integration.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/poiTl")
public class PoiTlController {


    @PostMapping("/getWordWithPoiTl")
    public void saveAndExportReview(HttpServletResponse response) throws IOException, InterruptedException {
        //数据封装data
        Map<String, Object> data = new HashMap<>();
        //1.插入字符串
        String format = String.format("这里来展示字符串的内容输出,时间:%s", DateUtil.now());
        data.put("stringValue", format);

        //2.插入表格
        List<RowRenderData> tableList = new ArrayList<>();
        //2.1 标题头
        RowRenderData row0 = Rows.of("序号", "学生姓名", "性别", "年龄").create();
        tableList.add(row0);
        for (int i = 1; i <= 5; i++) {
            RowRenderData row = Rows.of(String.valueOf(i),
                            "张三-" + i,
                            i % 2 == 0 ? "男" : "女",
                            "2" + i)
                    .create();
            tableList.add(row);
        }
        TableRenderData table = Tables.of(tableList.toArray(new RowRenderData[0])).create();
        data.put("tableValue", table);


        //3.插入饼状图
        ChartSingleSeriesRenderData pie = new ChartSingleSeriesRenderData();
        //设置饼状图标题
        pie.setChartTitle("天津北京人数(W)");
        //设置类型
        pie.setCategories(new String[]{"天津", "北京"});
        //设置对应类型的数据
        SeriesRenderData seriesRenderData = new SeriesRenderData();
        seriesRenderData.setValues(new Number[]{1364, 2183});
        pie.setSeriesData(seriesRenderData);
        data.put("circleValue", pie);


        //4.插入柱状图
        ChartMultiSeriesRenderData chartMultiSeriesRenderData = Charts.ofMultiSeries("长颈鹿生存现状", new String[]{"2018年", "2019年", "2020年"})
                .addSeries("成年鹿", new Integer[]{500, 600, 700})
                .addSeries("幼鹿", new Integer[]{200, 300, 400})
                .create();
        data.put("barValue", chartMultiSeriesRenderData);


        //5.插入图片（列表）
        List<Map<String, Object>> picList = new ArrayList<>();
        String picUrl = "https://picsum.photos/%s/%s";
        for (int i = 1; i <= 3; i++) {
            Map<String, Object> picMap = new HashMap<>();
            String url = String.format(picUrl, i * 100, i * 100);
            //根据url生成
            PictureRenderData pictureRenderData = Pictures
                    .ofUrl(url)
                    //设置图片大小
                    .size(i * 100, i * 100).create();
            picMap.put("pic", pictureRenderData);

            picList.add(picMap);
            //防止高频率访问图片
            Thread.sleep(2000);
        }
        data.put("picList", picList);


        InputStream is = null;
        is = this.getClass().getResourceAsStream("/templates/poi_templates.docx");
        XWPFTemplate compile = XWPFTemplate.compile(is);
        log.info("输出data============================：{}", data);
        XWPFTemplate render = compile.render(data);
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename="
                + URLEncoder.encode("poi-tl案例输出", "UTF-8") + ".docx");
        OutputStream out = response.getOutputStream();
        render.writeAndClose(out);
        is.close();

    }
}